package com.gientech.group9.entity.response;

import java.io.Serializable;


import com.gientech.group9.entity.enums.BaseEnum;
import lombok.Data;

/**
 * Created by wen on 2020-01-03
 */
@Data
public class ApiResponse<T> implements Serializable {

    public static final ApiResponse SUCCESS = new ApiResponse();
    public static final ApiResponse ERROR = new ApiResponse(ResponseInfo.ERROR);
    public static final ApiResponse LIMIT = new ApiResponse(ResponseInfo.LIMIT_ERROR);

    public static final ApiResponse PARAM_ERROR = new ApiResponse(ResponseInfo.PARAM_ERROR);

    private int code; // 返回响应码:200=成功返回，其他=错误返回

    private String msg; // 返回响应消息

    private T data; // 返回数据
    /**
     * 必须设置次属性否则远程调用可能转换失败！！！
     * JSON parse error: Unrecognized field "success"
     * com.gameco.me.common.core.api.ApiResponse["success"])
     */
    private boolean success;

    /**
     * 是否请求成功
     *
     * @return
     */
    public boolean isSuccess() {
        return 200 == code;
    }

    /**
     * 成功返回
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> success() {
        return new ApiResponse(ResponseInfo.SUCCESS.code, ResponseInfo.SUCCESS.msg);
    }

    /**
     * 成功返回
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> success(int code,String msg,T data) {
        return new ApiResponse(code,msg,data);
    }

    /**
     * 成功返回
     *
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse(ResponseInfo.SUCCESS.code, ResponseInfo.SUCCESS.msg, data);
    }

    /**
     * 错误返回
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> error(int code,String msg,T data) {
        return new ApiResponse(code,msg,data);
    }

    /**
     * 错误返回
     *
     * @param <T>
     * @return
     */
    public static <T> ApiResponse<T> error() {
        return new ApiResponse(ResponseInfo.ERROR.code, ResponseInfo.ERROR.msg);
    }

    /**
     * 错误返回
     *
     * @param message 错误备注
     * @param <T>
     * @return
     */

    public static <T> ApiResponse<T> error(String message) {
        return new ApiResponse(ResponseInfo.ERROR.code, message);
    }

    public static <T> ApiResponse<T> error(Integer code,String message) {
        return new ApiResponse(code, message);
    }

    public static <T> ApiResponse<T> error(BaseEnum baseEnum) {
        return new ApiResponse(baseEnum.getCode(), baseEnum.getMsg());
    }

    public static <T> ApiResponse<T> of(ResponseInfo responseInfo) {
        return new ApiResponse(responseInfo);
    }
    
    public static <T> ApiResponse<T> param(String message) {
        return new ApiResponse(ResponseInfo.PARAM_ERROR.getCode(), message);
    }

    public ApiResponse() {
        this(ResponseInfo.SUCCESS);
    }


    public ApiResponse(ResponseInfo responseInfo) {
        this.code = responseInfo.code;
        this.msg = responseInfo.msg;
    }

    public ApiResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public ApiResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


}
